package demo_table.dialog;

import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoComboBox;
import com.lovo.netCRM.component.LovoRadioButton;
import com.lovo.netCRM.component.LovoTxt;
import demo_table.bean.ClassBean;
import demo_table.bean.StudentBean;
import demo_table.frame.TableFrame;
import demo_table.util.DataUtil;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddStudentDialog extends JDialog {
    //输入框
    protected LovoTxt nameText,ageText;
    //单选
    private LovoRadioButton lovoRadioButton;
    //下拉菜单
    private LovoComboBox lovoComboBox;
    //添加按钮
    private LovoButton lovoButton;

    private TableFrame tableFrame;


    public AddStudentDialog(TableFrame tableFrame){
        this.tableFrame = tableFrame;
        this.setSize(500,500);
        this.setLayout(null);
        //this.setDefaultCloseOperation(3);

        nameText = new LovoTxt("学生姓名", 50, 50, this);

        lovoRadioButton = new LovoRadioButton("性别", new String[]{"男", "女"},50, 100, this);

        ageText = new LovoTxt("年龄", 50, 150, this);

        lovoComboBox = new LovoComboBox(DataUtil.getAllClasses(), "所在班级", 50, 200, this);

        lovoButton = new LovoButton("添加", 50, 250, this);

        lovoButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = nameText.getText();
                String gender = lovoRadioButton.getItem();

                if(name == null || "".equals(name) || ageText.getText() == null || "".equals(ageText.getText())){
                    JOptionPane.showMessageDialog(null, "修改失败， 姓名或年龄不能为空");
                    return;
                }
                int age = 0;
                try {
                     age = Integer.parseInt(ageText.getText());
                }catch (Exception e1){
                    JOptionPane.showMessageDialog(null, "修改失败， 年龄必须是数字");
                  //  e1.printStackTrace();
                    return;
                }
                ClassBean c = (ClassBean) lovoComboBox.getItem();

                StudentBean s = new StudentBean(DataUtil.getNextId(), name, gender, age, c);
                DataUtil.addStudent(s);

                tableFrame.getSdp().getLovoTable().updateLovoTable(DataUtil.getAllStduent());
                AddStudentDialog.this.dispose();
            }
        });

        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
}
